﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace RealTimeStocks
{
	public class BoolToVisibilityConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			bool visibility = false;
			if (value is bool?)
				visibility = ((bool?)value).Value;
			else if (value is bool)
				visibility = (bool)value;
			else
				return Visibility.Collapsed;

			bool revertResults;
			if (bool.TryParse(parameter.ToString(), out revertResults))
				if (!revertResults)
					visibility = !visibility;

			return visibility ? Visibility.Visible : Visibility.Collapsed;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			Visibility visibility = Visibility.Collapsed;
			if (value is Visibility)
				visibility = (Visibility)value;
			else
				return false;

			if (parameter is bool && !(bool)parameter)
				return (visibility == Visibility.Collapsed);
			else
				return (visibility == Visibility.Visible);
		}
	}
}
